package com.summer.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TaskRunnerPool {
	
	// 单线程池
	private static ExecutorService service = null;
	static {
		service = Executors.newSingleThreadExecutor();
	}

	public static void put(Runnable run) {
		if (run == null) {
			return;
		}
		// 执行
		service.execute(run);
	}

	public static void close() {
		if (service != null) {
			service.shutdown();
			service = null;
		}
	}

	public static void main(String[] args) throws Exception {
		TaskRunnerPool.put(new Runnable() {
			public void run() {
				throw new RuntimeException("hello");
			}
		});
		TaskRunnerPool.put(new Runnable() {
			public void run() {
				System.out.println("ok");
			}
		});
		// 多次执行
		Thread.currentThread().sleep(1000);
		// 关闭线程池
		TaskRunnerPool.close();
	}
}